$(document).ready(function(){
	//保存当前的标记数据
	var tree = $('#tree');//选定当前的树根
	var username=$("#user_name").html();//获取当前的用户名字
	var text;//当前的节点 
	//console.log(username);
	var supName=username;//保存当点节点的名字
	var identify;//查询当前用户的身份
	var curidentify;//标记当前的节点身份
	var role;//标记当前用户的下级的角色
	var descrption;//标记当前的用户描述
	
	$.ajax({
		type:"get",
		async: false,
		url:"Useridentify.action?username="+supName,
		success:function(result){
			identify=result;}	
	});
	console.log(username+":"+identify);//查询
	 if(identify=="admin")
	  {
	  role="group";
	  descrption="g";
	  }
 else if(identify=="group")
	  {
	  role="mentor";
	  descrption="m"
	  }
 else if(identify=="mentor")
	  {
	  role="student";
	  descrption="s"
	  }
//用户的标记变量.
	var pas_flag=true;
	var email_flag=true;
	var tel_flag=true;
	var active_flag=true;
	var user_flag=true;
	var treeflag;
	//查询用户树
	  var MonitArr=[];
		$.ajax({
			type:"post",
			async:false,
			contentType : "utf-8",
			url:"UserDataQuery.action?username="+username,
			success:function(result){
//				console.log(result);
				if(result==""){
					tree.jstree({
					    'core' : {
						"check_callback" : true,
					      'data' :
						  [{"id":"jh",
							"text":username,
							"icon":"fa fa-group",
							"state":"open",
							"children": initTree
							}]
							
					    },
					    "plugins" : ["state" ] ,
						'strings':{'Loading ...' : 'Please wait ...'}
					  });
					return;
				}
				var MonitArr= result.split(':');
				console.log(MonitArr);
			    var initTree = new Array(MonitArr.length);
			    for(var i = 0;i<MonitArr.length;i++){
			    	var d = {};
			    	d["text"] =MonitArr[i];
			    	d["id"] = i.toString();
			    	d["icon"] = "fa fa-user-plus";
			    	d["state"] = "open";
			    	initTree[i] = d;
			    	//console.log("当前一个中文用户是"+MonitArr[i]);
			    }
				/*tree.html文件中的文件夹树型列表js*/
				tree.jstree({
				    'core' : {
					"check_callback" : true,
				      'data' :
					  [{"id":"jh",
						"text":username,
						"icon":"fa fa-group",
						"state":"open",
						"children": initTree
						}]
						
				    },
					'strings':{'Loading ...' : 'Please wait ...'}
				  });
				return true;
			}
		});
		$("#new-user").hide();
		
		//树加载完成后触发事件，默认展开第一层节点
		tree.on("loaded.jstree", function (e, data) {
		  $.ajax({
			  type:"get",
			  url:"UserDataQuery.action?username="+username,
			  async:true,
			  success:function(result){
				  var MonitArr=result.split(',');
//				  for(var i = 0;i<MonitArr.length;i++){
//					  tree.jstree().create_node("jh",
//								{	"id":MonitArr[i],
//									"text":MonitArr[i],
//									"icon":"fa fa-user-plus",
//									},"last");
//				  }

				  tree.jstree().open_node("jh");
				  //默认展开树的第一层节点
				  //自动触发一次单击树节点事件
				  $(tree.jstree().select_node("jh")).click();
			  }
		  
    	});
		});		
  //节点的改变	
  var selected ='';
  var beforechange= '';
  tree.on("changed.jstree", function (e, data) {
	//被选中的节点编号
	selected =  data.selected;
	var selnode=data.instance.get_node(selected);
	text = tree.jstree().get_text(selected);
	//console.log(identify);
	//console.log(text);
	 $('#password').attr('readonly',false);
	//权限显示判断
	 $.ajax({
			type:"get",
			async: false,
			url:"Useridentify.action?username="+text,
			success:function(result){
			curidentify=result;
		   ;}	
	});//查询当前身份
	 if(identify=="admin"){
			$("#add-child-node1").show();
			$("#delete-node1,#delete-node2,#delete-node3,#add-child-node2,#add-child-node3").hide();
			if(curidentify!=role)
			{
			 $("#delete-node1,#delete-node2,#delete-node3,#add-child-node2,#add-child-node1,#add-child-node3").hide();
			}else
				{
				$("#add-child-node1").hide();
				$("#delete-node1").show();
			}
		}else if(identify=="group"){
			$("#add-child-node2").show();
			$("#delete-node1,#delete-node2,#delete-node3,#add-child-node1,#add-child-node3").hide();
			if(curidentify!=role)
			{
			 $("#delete-node1,#delete-node2,#delete-node3,#add-child-node2,#add-child-node1,#add-child-node3").hide();
			}else
				{
				$("#add-child-node2").hide();
				$("#delete-node2").show();
			}
		}else if(identify=="mentor")
		{
			$("#add-child-node3,#pre-update-data").show();
			$("#delete-node1,#delete-node2,#add-child-node1,#add-child-node2").hide();
			if(curidentify!=role)
			{
			 $("#delete-node1,#delete-node3,#delete-node2,#delete-node3,#add-child-node2,#add-child-node1,#add-child-node3").hide();
			}else
				{
				$("#add-child-node3").hide();
				$("#delete-node3").show();
			}
		}else
			{
			$("#delete-node1,#delete-node2,#delete-node3,#add-child-node2,#pre-update-data,#add-child-node1,#add-child-node3").hide();
			}
	 role="";
	 if(curidentify!=role) {
		 $("#default").show();
		 $("#delete-node1,#delete-node2,#delete-node3,#add-child-node2,#add-child-node1,#add-child-node3").hide();
		 $("#username,#identify,#superior,#password,#roleName,#email,#tel,#descripton,#active,#selector").attr("disabled",true);
		 $("#username,#identify,#superior,#password,#roleName,#email,#tel,#descripton,#active,#selector").css('backgroundColor', '#f3f3f3');
		 $("#new-data").hide();
		} else {
			 $("#default").show();
			 $("#username,#identify,#superior,#password,#roleName,#descripton,#selector").attr("disabled",true);
			 $("#username,#identify,#superior,#password,#roleName,#descripton,#selector").css('backgroundColor', '#f3f3f3');
			 $("#active,#email,#tel").attr("disabled",false);
			 $("#active,#email,#tel").css('backgroundColor', '#FFFFFF');
			 $("#new-data").show(); 
			}
	 $(".float-left1").css("width","20%");
	 
	 //清空错误数据
	 $("#tel_error").text("");
	 $("#email_error").text("");
	 $("#pas_error").text("");
	 $("#active_error").text("");
	// console.log("x"+curidentify);
	// console.log("x"+role);
	 //console.log("r"+identify);
	 if(curidentify==identify)
		 {
		 $("#password,#email,#tel").attr("disabled",false);
		 $("#password,#email,#tel").css('backgroundColor', '#FFFFFF');
		 $("#new-data").show();
		 switch(identify)
		 {
		 case "admin":
			 $("#add-child-node1").show();
			 break;
		 case "group":
			 $("#add-child-node2").show();
			 break;
		 case "mentor":
			 $("#add-child-node3").show();
			 break;
		 }
		 }
	 //以上是权限显示判断
	 
	 
	 
	
	$("#node-text").html("已选中：<small><i>"+text+"</i></small>");
	var user=[];
	//console.log(text);
	  $.ajax({
		  type:"get",
		  url:"userQuery.action?username="+text,
		  async:true,
		  contentType:'application/x-www-form-urlencoded; charset=utf-8',
		  success:function(result)
		  {
			  user=result.split(":");
			  $("#add-monitsite input").each(function(i,n){
				  $(this).val(user[i]);	  
			  });	
		  }
	  })
	  if(beforechange!=selected.toString() || selected!="jh"){
		  
		  $("#tree-menu").show();
		  if(selected=="jh"){
			  $("#input-place").css({"display":"block"});
			  return;
		  }else {
			  var MonitArr=[];
			  $("#input-place").css({"display":"block"});
		  $.ajax({
			  type:"get",
			  url:"userQuery.action?username="+text,
			  async:true,
			  success:function(result){
				  MonitArr=result.split(":");
					  var user=[];
					  $.ajax({
						  type:"get",
						  url:"userQuery.action?username="+text,
						  async:true,
						  success:function(result)
						  {
							  user=result.split(":");
							  $("#default").show();
							  $("#update-data").hide();
							  $("#add-monitsite input").each(function(i,n){
								  $(this).val(user[i]);	   
							  });
							  $.ajax({
								  type:"get",
								  url:"activeQuery.action?username="+text,
								  async:true,
								  success:function(result)
								  {
									  //console.log(result);
									  if(result=="yes")
										  {
									  $("#selector").val("yes");
										  }
									  else
										  {
									  $("#selector").val("no"); 
										  }
								  }
							  })  
						  }
					  })  
			  }
		  });
			beforechange=selected;
		}
	  }

	  
	  
	  
	  
	  
	  
	  
    var test = tree.jstree().get_json(); 
	var  string = JSON.stringify(test);
	//改变当前结点
	var MonitArr=[];
	tree.unbind("dblclick.jstree").bind("dblclick.jstree", function (obj) {
		//双击展开节点，再次双击关闭节点
		if(tree.jstree().is_open(selected)){tree.jstree().close_node(selected);}
		else{tree.jstree().open_node(selected);	
		if(!data.instance.is_leaf(selnode)) return;
			
		  if(selected=='jh'){ 
			  $.ajax({
				  type:"get",
				  async:true,
				  url:"UserDataQuery.action?username="+text,
				  contentType:'application/x-www-form-urlencoded; charset=utf-8',
				  success:function(result){
					  MonitArr=result.split(':');
					  for(var ix = 0;ix<MonitArr.length;ix ++){
							tree.jstree().create_node(selected,
							{
								"id":MonitArr[ix],
								"text":MonitArr[ix],
								"icon":"fa fa-user",
							},"last");	
						}
					  tree.jstree().open_node(selected);
				  }
			  });
		  }else
		  $.ajax({
			  type:"get",
			  async:true,
			  url:"UserDataQuery.action?username="+text,
			  success:function(result){
				 if(result==""||result==null){
					  alert("往后已无索引！");
				  }else{
				 MonitArr=result.split(':');
				  for(var ix = 0;ix<MonitArr.length;ix ++){
						tree.jstree().create_node(selected,
							{
								"id":MonitArr[ix],
								"text":MonitArr[ix],
								"icon":"fa fa-user",
							},"last");	
					}
				  tree.jstree().open_node(selected);
				  }
			  }
		  });
		}
	  });
  });
 
  
  //console.log(curidentify);
  



  
  
  
  
  //用户数据的新增
  $("#add-child-node1,#add-child-node2,#add-child-node3").unbind("click").bind("click",function(){
  $("#delete-node1,#delete-node2,#delete-node3,#add-child-node2,#add-child-node1,#add-child-node3").hide();
	  var user=[]; //清空当前选区
	  $("#add-monitsite input").each(function(i,n){
		  $(this).val(user[i]);
	  });
	  $("#username,#password,#email,#tel").attr("disabled",false);
		 $("#username,#password,#email,#tel").css('backgroundColor', '#FFFFFF');
	  $("#default").hide();
	  $("#update-data").show();//调出确认按钮
	  $("#new-data").hide();
	 // $("#mes-right1").css("float","right");
	 // $("#mes-right2").css("float","right");
	  //$("#mes-right1").css({"position":"absolute","top":"0px","left":"452px"});
	 // $("#mes-right2").css({"position":"absolute","left":"452px"});
	  $(".float-left1").css("width","40%");
  });
	  
	

  
  //新增用户
  $("#update-data").unbind("click").bind("click",function(){
	  	var name=$("#username").val();
		var pas=$("#password").val();
		var tel=$("#tel").val();
		var email=$("#email").val();
		if(name=='' || name==undefined || name==null)//电话号码的空值验证
		{
		user_flag=false;
		}
		else
			{
			user_flag=true;
			}
		if(password=='' || password==undefined || password==null)//电话号码的空值验证
		{
		pas_flag=false;

		}
		else
			{
			pas_flag=true;
			}
		if(tel=='' || tel==undefined || tel==null)//电话号码的空值验证
		{
		tel_flag=false;

		}
		else
			{
			tel_flag=true;
			}
		if(email=='' || email==undefined || email==null)//电话号码的空值验证
		{
		email_flag=false;

		}
		else
			{
			email_flag=true;
			}
		/*console.log("user"+user_flag);
	  	console.log("email:"+email_flag);
		console.log("tel:"+tel_flag);
		console.log("active:"+active_flag);
		console.log("pas:"+pas_flag);*/
		if(email_flag&&user_flag&&tel_flag&&pas_flag)
			{
			var user = $("#username").val();
			if(curidentify=="admin")
			  {
			  role="group";
			  descrption="g";
			  $.ajax({
			   	  type:"get",
			   	  async:false,
				  url:"newQuery.action",
				  data:{
					 "username":$("#username").val(),
					 "password":$("#password").val(),
					 "tel":$("#tel").val(),
					 "email":$("#email").val(),
					 "identify":descrption,
					 "active":"yes",
					 "superior":tree.jstree().get_text(selected),
					 "roleName":role
				  },
				  success:function(result){
					  if(result=="failure"){
						  alert("增加失败！");
					  }else{
						  alert("增加成功！");
//							  location.reload();
							  tree.jstree().create_node(selected,
										{
											"id":user+"str",
											"text":user,
											"icon":"fa fa-group",
										},"last");	
									}
						  //添加新节点跳到新节点
						  tree.jstree().deselect_node(selected);
						  //自动触发一次单击树节点事件
						  $(tree.jstree().select_node(user+"str")).click();
				  }
			  });
			  }
		 else if(curidentify=="group")
			  {
			  role="mentor";
			  descrption="m";
			  $.ajax({
			   	  type:"get",
			   	  async:false,
				  url:"newQuery.action",
				  data:{
					 "username":$("#username").val(),
					 "password":$("#password").val(),
					 "tel":$("#tel").val(),
					 "email":$("#email").val(),
					 "identify":descrption,
					 "active":"yes",
					 "superior":tree.jstree().get_text(selected),
					 "roleName":role
				  },
				  success:function(result){
					  if(result=="failure"){
						  alert("增加失败！");
					  }else{
						  alert("增加成功！");
//							  location.reload();
							  tree.jstree().create_node(selected,
										{
											"id":user+"str",
											"text":user,
											"icon":"fa fa-user-plus",
										},"last");	
									}
						  //添加新节点跳到新节点
						  tree.jstree().deselect_node(selected);
						  //自动触发一次单击树节点事件
						  $(tree.jstree().select_node(user+"str")).click();
				  }
			  });
			  }
		 else if(curidentify=="mentor")
			  {
			  role="student";
			  descrption="s";
			  $.ajax({
			   	  type:"get",
			   	  async:false,
				  url:"newQuery.action",
				  data:{
					 "username":$("#username").val(),
					 "password":$("#password").val(),
					 "tel":$("#tel").val(),
					 "email":$("#email").val(),
					 "identify":descrption,
					 "active":"yes",
					 "superior":tree.jstree().get_text(selected),
					 "roleName":role
				  },
				  success:function(result){
					  if(result=="failure"){
						  alert("增加失败！");
					  }else{
						  alert("增加成功！");
//							  location.reload();
							  tree.jstree().create_node(selected,
										{
											"id":user+"str",
											"text":user,
											"icon":"fa fa-user-plus",
										},"last");	
									}
						  //添加新节点跳到新节点
						  tree.jstree().deselect_node(selected);
						  //自动触发一次单击树节点事件
						  $(tree.jstree().select_node(user+"str")).click();
				  }
			  });
			  }
		  
	}
		else
			{
			alert("增加失败，请正确填写资料");
			return;
			}
  })
//以上是增加
  
  //删除操作定版
  $("#delete-node1,#delete-node2,#delete-node3").unbind("click").bind("click",function(){
	
		 var curName=text;
		// console.log(curidentify);
	
		 //console.log(role);
		 if(role!=curidentify)
			{
			 alert("只能删除下属用户");
			}
		 else if(text==username)
			 {
			 alert("不能删除当前登录用户");
			 }
		 else
			 {
			 
			 flag=confirm("确定要删除"+text+"吗？");
			 if(flag)
			{
			 $.ajax({
			   	  type:"get",
			   	  async: false,
				  url:"deleteQuery.action?username="+curName,
				  async:true,
				  success:function(result){
					  if(result=="failure"){
						  alert("删除失败！");
					  }else{
						  alert("删除成功！");
						  var parent = tree.jstree().get_parent(selected);
						  tree.jstree().delete_node(selected);
						  //自动触发一次单击树节点事件
						  tree.jstree().select_node(parent).click();
//						  location.reload();
					  }
				  }
			  });
			}
		}
	})
//修改数据的代码
  $("#new-data").unbind("click").bind("click",function(){
	  var curactive=$("#selector").val();
	  var curName=text;
	  var id=null;
	  var flagup=confirm("确定要修改么？");

		if(name=='' || name==undefined || name==null)//电话号码的空值验证
		{
		user_flag=false;
		}
		else
			{
			user_flag=true;
			}
		if(password=='' || password==undefined || password==null)//电话号码的空值验证
		{
		pas_flag=false;

		}
		else
			{
			pas_flag=true;
			}
		if(tel=='' || tel==undefined || tel==null)//电话号码的空值验证
		{
		tel_flag=false;

		}
		else
			{
			tel_flag=true;
			}
		if(email=='' || email==undefined || email==null)//电话号码的空值验证
		{
		email_flag=false;

		}
		else
			{
			email_flag=true;
			}
		if(flagup)
			{
			$.ajax({
				type:"get",
				async: false,
				url:"idQuery.action?username="+curName,
				success:function(result){
					id=result;}	
			});//获取当前数据的id值用来就该
			/*console.log("email:"+email_flag);
			console.log("tel:"+tel_flag);
			console.log("active:"+active_flag);
			console.log("pas:"+pas_flag);*/
		if(email_flag&&tel_flag&&pas_flag&&active_flag)
			{
		  $.ajax({
		   	  type:"get",
		   	  async:true,
			  url:"updateQuery.action",
			  data:{
				 "username":$("#username").val(),
				 "id":id,
				 "password":$("#password").val(),
				 "tel":$("#tel").val(),
				 "email":$("#email").val(),
				 "active":curactive,
			  },
			  success:function(result){
				  if(result=="failure"){
					  alert("修改失败！");
					  return;
				  }else{
					  alert("修改成功！");
					  location.reload();
				  }
			  }  	
		  });
		}
		else
			{
			alert("修改失败，请正确输入数据");
			return;
			}
		}
  })
  
//输入检查的代码如下
$("#tel").keyup(function(){
	
	
	var tel=$("#tel").val();
	if(tel=='' || tel==undefined || tel==null)//电话号码的空值验证
	{
	$("#tel_error").text("电话号码必填");
	tel_flag=false;
	}
	else
	{	
		$("#tel_error").text("");
		tel_flag=true;
	}
	 var telrule= /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
	 var telflag=telrule.test(tel)
	 // console.log(telflag);
	  if(!telflag){ //检验格式
		  $("#tel_error").text("格式错误");
		  tel_flag=false;
		}
	  else
		{
		$("#tel_error").text("");
		tel_flag=true;
		}
})
//以上是电话号码的验证
//下面是邮箱的代码验证
$("#email").keyup(function(){
	var email=$("#email").val();
	if(email=='' || email==undefined || email==null)//电话号码的空值验证
	{
	$("#email_error").text("邮箱不能为空");
	email_flag=false;
	}
	else
	{
		$("#email_error").text("");
		email_flag=true;
	}
	 var emailrule=/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	 var emailflag=emailrule.test(email)
	  if(!emailflag){ //检验格式
		  $("#email_error").text("邮箱格式错误");
		  email_flag=false;
		}
	  else
		{
		$("#email_error").text("");
		email_flag=true;
		}
})
//以上是邮箱的验证
//以下是密码的验证
$("#password").keyup(function(){
	
	var password=$("#password").val();
	if(password=='' || password==undefined || password==null)//电话号码的空值验证
	{
	$("#pas_error").text("密码不能为空");
	pas_flag=false;
	}
	else
	{
		$("#pas_error").text("");
	}
	 var passwordrule= /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,17}$/;
	 var passwordflag=passwordrule.test(password)
	 // console.log(telflag);
	  if(!passwordflag){ //检验格式
		  $("#pas_error").text("6-18位字符");  
		  passwordflag=false;
		}
	  else
		{
		$("#pas_error").text("");
		pas_flag=true;
		}
})
//以上是密码的验证
//以下是状态的验证
$("#active").keyup(function(){
	
	var active=$("#active").val();
	if(active=='' || active==undefined || active==null)//电话号码的空值验证
	{
	$("#active_error").text("状态不能为空");
	active_flag=false;
	}
	else
	{
		$("#active_error").text("");
	}
	if(active!="yes"&&active!="no")
	{
		$("#active_error").text("状态只能为yes或no");
		active.flag=false;
	}
	else
	{
		$("#active_error").text("");
		active_flag=true;
	}
})
//以上是状态的验证
//以下是用户名验证
$("#username").keyup(function(){
	
	curname=$("#username").val();

	console.log(curname);
	if(curname=='' || curname==undefined || curname==null)//电话号码的空值验证
	{
	$("#username").text("用户名不能为空");
	user_flag=false;
	}
	else
	{
		$("#user_error").text("");
		user_flag=true;
	}
	  $.ajax({
			type:"get",
			async: false,
			url:"check_username.action?uname="+curname,
			success:function(result){
				//console.log(result);
				if(result=="success")
					{
						$("#user_error").text("");
						user_flag=true;
					}
				else
					{
					$("#user_error").text("已存在的用户名");
					user_flag=false;
					}
			}
		});
})
//以上是用户名验证
});

